package dreamCookGame;

import java.io.*;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class GameState {

	public Inventory _inventory;
	public MixingBowl _mixingBowl;
	public BakingPan _bakingPan;
	public FryingPan _fryingPan;
	public Oven _oven;
	public int _currentPlotPoint;
	public Location _currentLocation;
	public Location _previousLocation;
	PlotPointReference _plotPointReference;
    JFileChooser _chooser;
	
	public GameState() {
		_inventory = new Inventory();
		_mixingBowl = new MixingBowl();
		_bakingPan = new BakingPan();
		_fryingPan = new FryingPan();
		_oven = new Oven();
		_currentPlotPoint = 3;
		_plotPointReference = new PlotPointReference();
		_chooser = new JFileChooser();
	}

	public void updateCurrentLocationFromPlotPoint() {
		if(_currentLocation._name.equalsIgnoreCase("Kitchen")) {
			_currentLocation._itemsBack = _plotPointReference._plotPoints[_currentPlotPoint]._kitchenContent._itemsBack;
			_currentLocation._itemsFront = _plotPointReference._plotPoints[_currentPlotPoint]._kitchenContent._itemsFront;
			_currentLocation._characters = _plotPointReference._plotPoints[_currentPlotPoint]._kitchenContent._characters;
		}
		else if(_currentLocation._name.equalsIgnoreCase("LivingRoom")) {
			_currentLocation._itemsBack = _plotPointReference._plotPoints[_currentPlotPoint]._livingRoomContent._itemsBack;
			_currentLocation._itemsFront = _plotPointReference._plotPoints[_currentPlotPoint]._livingRoomContent._itemsFront;
			_currentLocation._characters = _plotPointReference._plotPoints[_currentPlotPoint]._livingRoomContent._characters;
		}
		else if(_currentLocation._name.equalsIgnoreCase("Market")) {
			_currentLocation._itemsBack = _plotPointReference._plotPoints[_currentPlotPoint]._marketContent._itemsBack;
			_currentLocation._itemsFront = _plotPointReference._plotPoints[_currentPlotPoint]._marketContent._itemsFront;
			_currentLocation._characters = _plotPointReference._plotPoints[_currentPlotPoint]._marketContent._characters;
		}
		else if(_currentLocation._name.equalsIgnoreCase("Farm")) {
			_currentLocation._itemsBack = _plotPointReference._plotPoints[_currentPlotPoint]._farmContent._itemsBack;
			_currentLocation._itemsFront = _plotPointReference._plotPoints[_currentPlotPoint]._farmContent._itemsFront;
			_currentLocation._characters = _plotPointReference._plotPoints[_currentPlotPoint]._farmContent._characters;
		}
		else if(_currentLocation._name.equalsIgnoreCase("Island")) {
			_currentLocation._itemsBack = _plotPointReference._plotPoints[_currentPlotPoint]._islandContent._itemsBack;
			_currentLocation._itemsFront = _plotPointReference._plotPoints[_currentPlotPoint]._islandContent._itemsFront;
			_currentLocation._characters = _plotPointReference._plotPoints[_currentPlotPoint]._islandContent._characters;
		}
		else if(_currentLocation._name.equalsIgnoreCase("ChefsHut")) {
			_currentLocation._itemsBack = _plotPointReference._plotPoints[_currentPlotPoint]._chefsHutContent._itemsBack;
			_currentLocation._itemsFront = _plotPointReference._plotPoints[_currentPlotPoint]._chefsHutContent._itemsFront;
			_currentLocation._characters = _plotPointReference._plotPoints[_currentPlotPoint]._chefsHutContent._characters;
		}
	}
	
	public void loadCurrentLocation(Location location) {
		_previousLocation = _currentLocation;
		_currentLocation = location;
	}
	
	public void loadPreviousLocationAsCurrent() {
		Location tempLocation = _currentLocation;
		_currentLocation = _previousLocation;
		_previousLocation = tempLocation;
		
	}
	
	public void saveState(){

		File saveFile = getSaveFile();
		if(saveFile!=null){
			if( !saveFile.exists() ) {
				saveToFile(saveFile);
			}
			else {
				
				int selection = JOptionPane.showConfirmDialog(null, "Are you sure you want to overwrite this saved game?");
				
				if( selection == JOptionPane.YES_OPTION ) {
					
					saveToFile(saveFile);
					
				}
				else if( selection == JOptionPane.NO_OPTION ) {
					
					saveState();
					
				}
			}
		}
	}
	
	private void saveToFile( File saveFile ) {
		
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(saveFile));
			out.write("DreamCook\n");
			out.write("pp "+_currentPlotPoint+"\n");
			out.write("loc "+_previousLocation._name+"\n");
			int size = _inventory._items.length;
		
			for(int i=0; i<size; i++) {
				out.write("inv "+_inventory._items[i]._name+"\n");
			}
			
			out.close(); 
		
		} catch (IOException e) {
		
			JOptionPane.showMessageDialog(null,
					  "Unable to save to file",
					  "Save Game Error",
					  JOptionPane.ERROR_MESSAGE);
		
			e.printStackTrace();
		
		}
	}
	
	public void loadState() {
		
			File openFile = getOpenFile();
			
			if(openFile!=null){
					
				try {
						
					BufferedReader in = new BufferedReader( new FileReader(openFile) );
					
					String line;

					if((line = in.readLine()).equals("DreamCook")){
						for(int i=0; i<_inventory._items.length; i++){
							_inventory._items[i]._isInInventory = false;
						}
						
							_inventory._items = new Item[0];
						
						while( ( line = in.readLine() ) != null ) {
							
							String[] part = line.split(" ");
							
							if ( part[0].equals("inv" ) ) {

								boolean found = false;
								
								for ( int i=0; i<=_currentPlotPoint; i++ ) {

									if ( !found ) {
										
										//check against chef's hut items
										found = compareContentAndAddToInventory(_plotPointReference._plotPoints[i]._chefsHutContent, part[1]);
			
										if(!found) {
			
											//Check against kitchen items
											found = compareContentAndAddToInventory(_plotPointReference._plotPoints[i]._kitchenContent, part[1]);
											
										}
										
										if(!found) {
			
											//Check against living room items
											found = compareContentAndAddToInventory(_plotPointReference._plotPoints[i]._livingRoomContent, part[1]);
											
										}
										
										if(!found) {
			
											//Check against market items
											found = compareContentAndAddToInventory(_plotPointReference._plotPoints[i]._marketContent, part[1]);
											
										}
			
										if(!found) {
			
											//Check against farm items
											found = compareContentAndAddToInventory(_plotPointReference._plotPoints[i]._farmContent, part[1]);
											
										}
										
										if(!found) {
			
											//Check against island items
											found = compareContentAndAddToInventory(_plotPointReference._plotPoints[i]._islandContent, part[1]);
											
										}
										
									} else {
										
										break;
										
									}
										
								}
							}
							else if(part[0].equals("pp") ) {
								_currentPlotPoint = Integer.parseInt(part[1]);
							}
							else if( part[0].equals("loc") ) {
								if( part[1].equals("Kitchen") ) {
									Location tempLocation = _currentLocation;
									_previousLocation = _plotPointReference._kitchen;
									_currentLocation = _previousLocation;
									updateCurrentLocationFromPlotPoint();
									_currentLocation = tempLocation;
									}
								else if( part[1].equals("LivingRoom") ) {
									Location tempLocation = _currentLocation;
									_previousLocation = _plotPointReference._livingRoom;
									_currentLocation = _previousLocation;
									updateCurrentLocationFromPlotPoint();
									_currentLocation = tempLocation;
								}
								else if( part[1].equals("Market") ) {
									Location tempLocation = _currentLocation;
									_previousLocation = _plotPointReference._market;
									_currentLocation = _previousLocation;
									updateCurrentLocationFromPlotPoint();
									_currentLocation = tempLocation;
								}
								else if( part[1].equals("Farm") ) {
									Location tempLocation = _currentLocation;
									_previousLocation = _plotPointReference._farm;
									_currentLocation = _previousLocation;
									updateCurrentLocationFromPlotPoint();
									_currentLocation = tempLocation;
								}
								else if( part[1].equals("Pommajuice") ) {
									Location tempLocation = _currentLocation;
									_previousLocation = _plotPointReference._island;
									_currentLocation = _previousLocation;
									updateCurrentLocationFromPlotPoint();
									_currentLocation = tempLocation;
								}
								else if( part[1].equals("ChefsHut") ) {
									Location tempLocation = _currentLocation;
									_previousLocation = _plotPointReference._chefsHut;
									_currentLocation = _previousLocation;
									updateCurrentLocationFromPlotPoint();
									_currentLocation = tempLocation;
								}
								
							}
						
						}
						JOptionPane.showMessageDialog(null,
								  "Game Loaded",
								  "Message",
								  JOptionPane.INFORMATION_MESSAGE);
					}
					else {
						JOptionPane.showMessageDialog(null,
													  "Not a Dream Cook Game save file",
													  "Load Game Error",
													  JOptionPane.ERROR_MESSAGE);
					}
					
					in.close();
					
				}
				
				catch ( IOException e ) {
					JOptionPane.showMessageDialog(null,
							  "Unable to read file",
							  "Load Game Error",
							  JOptionPane.ERROR_MESSAGE);					
					e.printStackTrace();
					
				}
				
			}
			
		}

	
	private boolean compareContentAndAddToInventory(SceneContent sceneContent, String compare){
		
		boolean found = false;

		if( sceneContent != null ) {
			
			Item[] checkedItemsList = sceneContent._itemsBack;
			
			if( checkedItemsList != null ) {
				
				for( int j=0; j<checkedItemsList.length; j++ ) {
					
					Item checkedItem = checkedItemsList[j];
					String name = checkedItem._name;
					
					if( compare.equals(name) ) {
						
						_inventory.addItemToInventory(checkedItem);
						checkedItem._isInInventory = true;
						found = true;
						break;
					}
				}
			}
			
			if(!found){
				checkedItemsList = sceneContent._itemsFront;
				
				if( checkedItemsList != null ) {
					
					for( int j=0; j<checkedItemsList.length; j++ ) {
						
						Item checkedItem = checkedItemsList[j];
						
						if( compare.equals(checkedItem._name) ) {
							
							_inventory.addItemToInventory(checkedItem);
							checkedItem._isInInventory = true;
							found = true;
							break;
							
						}
						
					}
					
				}
				
			}
			
		}
		
		return found;
		
	}
	
	private File getOpenFile(){
		
		File file = null;
		
		if( _chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
			
			file = _chooser.getSelectedFile();
			
		}
		
		return file;
		
	}
	
	private File getSaveFile(){
		File file = null;
		
		if( _chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION ) {
			
			file = _chooser.getSelectedFile();
			
			String fileString = file.toString();
			
			if( !fileString.endsWith(".txt") ) {
				
				fileString=fileString+".txt";
				
			}
			
			file = new File(fileString);
		}
		
		return file;
	}
}
